package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dao.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录认证
     * @param telephone
     * @param validateCode
     * @return
     */
    public Result login(String telephone, Integer validateCode) {

        //校验用户输入的短信验证码是否正确，如果验证码错误则登录失败
         Integer code = (Integer)redisTemplate.opsForValue().get(telephone);
         if (code != null && code.equals(validateCode)){
             return new Result(true, MessageConstant.LOGIN_SUCCESS);
         }
         // 如果验证码正确，则判断当前用户是否为会员，如果不是会员则自动完成会员注册
        Member member = memberMapper.findByTelephone(telephone);
        if ( member == null){
            member = new Member();
            member.setPhoneNumber(telephone);
            memberMapper.add(member);
        }
        return new Result(false, MessageConstant.VALIDATECODE_ERROR);
    }
}
